package com.lifo.utils.common.http;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
public class HttpClientUtil {
    private static final OkHttpClient client = new OkHttpClient();

    static {
        client.newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS);
    }


    /**
     * Post请求发送JSON数据
     * 请求Url
     * 请求的JSON
     */
    public static String doPostJson(String url, String jsonParams) throws IOException {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * 带参post http接口
     * @param url
     * @param parameter
     * @return
     * @throws IOException
     */
    public static String doPost(String url, Map parameter) throws IOException {

        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        Iterator iterator = parameter.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next().toString();
            formBodyBuilder.add(key, parameter.get(key).toString());
        }
        RequestBody requestBody = formBodyBuilder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    public static String doGet(String url){
        OkHttpClient okHttpClient=new OkHttpClient();
        //构建请求项
        Request request=new Request.Builder()
                .get()
                .url(url.toString())
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * http 请求动作
     * @param client
     * @param request
     * @return
     */
    public static String request(OkHttpClient client, Request request){
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return request.url()+" request error";
        }
    }

}
